﻿using System.Collections.Generic;
using System.Linq;
using UnityEditor;

namespace Global
{
    public class EditorExtension
    {
        const string KK_DEBUG = "KK_DEBUG";
        const string KK_RELEASE = "KK_RELEASE";
        static List<string> list = new List<string>();

        static void GetDefines()
        {
            string[] defines;
            PlayerSettings.GetScriptingDefineSymbolsForGroup(GetBuildTargetGroup(), out defines);
            list = defines.ToList<string>();
        }

        [MenuItem("Tools/GamePattern/Debug")]
        static void DebugPattern()
        {
            GetDefines();
            if (!list.Contains(KK_DEBUG))
            {
                list.Add(KK_DEBUG);
            }

            if (list.Contains(KK_RELEASE))
            {
                list.Remove(KK_RELEASE);
            }

            PlayerSettings.SetScriptingDefineSymbolsForGroup(GetBuildTargetGroup(), list.ToArray<string>());
        }

        [MenuItem("Tools/GamePattern/Debug", true)]
        static bool DebugPatternValidate()
        {
            GetDefines();
            return !list.Contains(KK_DEBUG);
        }

        [MenuItem("Tools/GamePattern/Release")]
        static void ReleasePattern()
        {
            GetDefines();
            if (!list.Contains(KK_RELEASE))
            {
                list.Add(KK_RELEASE);
            }

            if (list.Contains(KK_DEBUG))
            {
                list.Remove(KK_DEBUG);
            }

            PlayerSettings.SetScriptingDefineSymbolsForGroup(GetBuildTargetGroup(), list.ToArray<string>());
        }

        [MenuItem("Tools/GamePattern/Release", true)]
        static bool ReleasePatternValidate()
        {
            GetDefines();
            return !list.Contains(KK_RELEASE);
        }

        static BuildTargetGroup GetBuildTargetGroup()
        {
            BuildTargetGroup ret = BuildTargetGroup.Standalone;
#if UNITY_ANDROID
            ret = BuildTargetGroup.Android;
#elif UNITY_IOS
         ret = BuildTargetGroup.iOS;
#endif
            return ret;
        }
    }
}